//
//  XLStatusTableViewCell.swift
//  XLVeibo
//
//  Created by macbook on 16/6/27.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

import UIKit
import SnapKit

class XLStatusTableViewCell: UITableViewCell {
    // 纪录底部toolbar顶部约束
    var toolBarTopConstraint:Constraint?
    
    
    //给外界cell设置数据
    var status:XLStatus?{
    
        didSet{
        
            //给原创微博赋值
            statusOriginalView.status = status
            
            //先卸载top约束
            toolBarTopConstraint?.uninstall()
            
            //没有转发微博
            if status?.retweeted_status == nil {
                
                statusRetweet.hidden = true
                
                statusTools.snp_makeConstraints(closure: { (make) in
                    
                    toolBarTopConstraint = make.top.equalTo(statusOriginalView.snp_bottom).constraint
                })
                
            //有转发微博
            }else
            {
                //赋值
                statusRetweet.status = status
                statusRetweet.hidden = false
                
                statusTools.snp_makeConstraints(closure: { (make) in
                    
                    toolBarTopConstraint = make.top.equalTo(statusRetweet.snp_bottom).constraint
                })
                
            }
            
            //给tob赋值
            statusTools.status = status
        }
    }
    
    

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
      super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    
    //设置视图
    private func setupUI(){
    
        self.backgroundColor = UIColor.whiteColor()
        //添加子控件
        contentView.addSubview(statusOriginalView)
       
        contentView.addSubview(statusRetweet)
        
         contentView.addSubview(statusTools)
    
        
//        添加约束
        statusOriginalView.snp_makeConstraints { (make) in
            
            make.left.right.equalTo(contentView)
           make.top.equalTo(contentView).offset(8)

        }
        
        //转发微博
        statusRetweet.snp_makeConstraints { (make) in
            
            make.top.equalTo(statusOriginalView.snp_bottom)
            make.left.right.equalTo(contentView)
            
        }
        
        statusTools.snp_makeConstraints { (make) in
        
            make.left.right.equalTo(contentView)
           
            //记录一下高
           toolBarTopConstraint = make.top.equalTo(statusRetweet.snp_bottom).constraint
            make.height.equalTo(35)
            
        }

        
        
        
//        约束自己
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(statusTools.snp_bottom)
        }

        
                
    }
    
    
    //懒加载子控件
    private lazy var statusOriginalView:XLStatusOriginalView = XLStatusOriginalView()
    
    //懒加载底部tooble
    private lazy var statusTools:XLStatusToolbar  = XLStatusToolbar()
    
   
    private lazy var statusRetweet:XLStatusRetweetView = XLStatusRetweetView()
        
    
}
